package com.mifengs.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.BannerBean;
import com.mifengs.mall.loader.GlideImageLoader;
import com.mifengs.mall.util.ToIntentUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.view.BannerViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/12/20.
 */

public class HomeBannerAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder>  {

    private Context mContext;

    private List<BannerBean> bannerBeanList;

    public HomeBannerAdapter(Context context) {
        this.mContext = context;
    }

    public void setBannerBeanList(List<BannerBean> bannerBeanList) {
        this.bannerBeanList = bannerBeanList;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BannerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_floor_banner,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof BannerViewHolder){
            BannerViewHolder viewHolder = (BannerViewHolder) holder;
            Banner banner = viewHolder.banner;
            banner.setIndicatorGravity(BannerConfig.RIGHT);
            if(bannerBeanList!=null){
                banner.setImages(bannerBeanList)
                        .setImageLoader(new GlideImageLoader())
                        .setDelayTime(5000)
                        .setOnBannerListener(mListener)
                        .start();
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class BannerViewHolder extends RecyclerView.ViewHolder{

        private Banner banner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }

    private OnBannerListener mListener = new OnBannerListener() {
        @Override
        public void OnBannerClick(int position) {
            ToIntentUtil.toCommonIntent(bannerBeanList.get(position).getActionUrl(),mContext);
        }
    };
}
